package com.joelaws.prototype.client.manage;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.Checkbox;
import com.gwtext.client.widgets.form.FormPanel;
import com.gwtext.client.widgets.form.event.CheckboxListenerAdapter;
import com.gwtext.client.widgets.layout.AnchorLayout;
import com.joelaws.prototype.client.ScreenManager;
import com.joelaws.prototype.client.data.DatingService;
import com.joelaws.prototype.client.data.MessagingService;
import com.joelaws.prototype.client.data.NetworkingService;
import com.joelaws.prototype.client.data.User;
import com.joelaws.prototype.client.rpc.Manage;
import com.joelaws.prototype.client.rpc.ManageAsync;

public class ManageHomeTab extends Panel {

	final private User user;

	public ManageHomeTab(User user) {
		this.user = user;
		setBorder(false);
		setFrame(true);
		setTitle("Manage Home");
		setLayout(new AnchorLayout());

		add(getFormPanel());
	}

	public Panel getFormPanel() {
		final FormPanel panel = new FormPanel();
		panel.setWidth(300);

		final Checkbox networking = new Checkbox("Networking Service");
		final Checkbox dating = new Checkbox("Dating Service");
		final Checkbox messaging = new Checkbox("Messaging Service");

		if (user.getDatingService() != null) {
			dating.setValue(true);
		}

		if (user.getMessagingService() != null) {
			messaging.setValue(true);
		}

		if (user.getNetworkingService() != null) {
			networking.setValue(true);
		}

		CheckboxListenerAdapter listener = new CheckboxListenerAdapter() {

			public void onCheck(Checkbox field, boolean checked) {
				if (field == dating) {
					if (!networking.getValue() || !messaging.getValue()) {
						messaging.setValue(true);
						networking.setValue(true);
						panel.doLayout();
					} else {
						panel.doLayout();
					}
				}
			}
		};

		panel.add(networking);
		panel.add(dating);
		panel.add(messaging);

		networking.addListener(listener);
		messaging.addListener(listener);
		dating.addListener(listener);

		Button values = new Button("Save", new ButtonListenerAdapter() {

			public void onClick(Button button, EventObject e) {
				System.out.println("clicked " + networking.getValueAsString());
				System.out.println("clicked " + dating.getValueAsString());
				System.out.println("clicked " + messaging.getValueAsString());

				ManageAsync instance = Manage.Util.getInstance();

				boolean changed = false;

				if (networking.getValue() && user.getNetworkingService() == null) {
					changed = true;
					user.setNetworkingService(new NetworkingService(user.getUserID(), null, null));
				}

				if (dating.getValue() && user.getDatingService() == null) {
					changed = true;
					user.setDatingService(new DatingService(user.getUserID(), null));
				}
				if (messaging.getValue() && user.getMessagingService() == null) {
					changed = true;
					user.setMessagingService(new MessagingService(user.getUserID()));
				}

				if (changed) {
					instance.updateUser(user, new AsyncCallback<Object>() {

						public void onFailure(Throwable error) {
							Window.alert("Error occured:" + error.toString());
						}

						public void onSuccess(Object obj) {
							System.out.println("Saved services ");
							MessageBox.alert("Saved");
							ScreenManager.instance().showScreen(new ManagePanel(user));
						}
					});
				}
			}
		});

		panel.addButton(values);

		return panel;

	}
}
